﻿using Api.Common;
using AppManager.IBLL;
using Microsoft.Practices.Unity;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace AppManager.Web
{
    public class IndexUrlProvider : BaseProvider
    {
        public IAppService AppService { get { return DependencyResolver.Current.GetService<IAppService>(); } }

        public override RouteData GetMyRouteData(HttpContextBase httpContext)
        {
            //这里一定要注意的是，就算你是iis上的一个应用程序，不是在根目录下的话，获取的VirtualPath只和程序中相关

            //我以为是这个样子的，但其实是~/bigline,~/home/qrcode/1
            //~/App/bigline
            //~/App/bigline            
            //LogHelper.WriteInfoLog(VirtualPath);

            var path = VirtualPath.Replace("~/", "");
            if (!path.Contains('/'))
            {
                var regex = new Regex("~/([a-zA-Z0-9])*");
                var match = regex.Match(VirtualPath);
                if (match.Success)
                {
                    string identity = match.Groups[0].Value.ToLower().Replace("~/", "");

                    string controllerName = "home", actionName = "index";
                    if (identity.StartsWith("scan"))
                    {
                        actionName = "scan";
                        identity = identity.Substring(4);
                    }
                    else if (identity.StartsWith("test"))
                    {
                        actionName = "test";
                        identity = identity.Substring(4);
                    }

                    var data = new RouteData(this, new MvcRouteHandler());
                    data.Values.Add("controller", controllerName);
                    data.Values.Add("action", actionName);
                    data.Values.Add("id", InitId(identity));

                    return data;
                }
            }

            return null;
        }

        public override VirtualPathData GetMyVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            //首页直接返回一个/channelId
            if (VirtualControllerName == "home")
            {
                var id = GetValue(values, "id");
                var identity = GetIdentity(id);
                if (VirtualActionName == "index")
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append(identity);
                    return new VirtualPathData(this, sb.ToString());
                }
                else if (VirtualActionName == "scan")
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("scan{0}", identity);
                    return new VirtualPathData(this, sb.ToString());
                }
                else if (VirtualControllerName == "test")
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("test{0}", identity);
                    return new VirtualPathData(this, sb.ToString());
                }
            }

            return null;
        }

        private int InitId(string identity)
        {
            if (string.IsNullOrEmpty(identity.Trim())) return 0;
            //根据identity 去查询
            var app = AppService.GetModel(s => s.Identity == identity);
            if (app == null) return 0;
            return app.Id;
        }

        private string GetIdentity(string appId)
        {
            int id = 0;
            int.TryParse(appId, out id);
            if (string.IsNullOrEmpty(appId.Trim())) return string.Empty;

            //居然不能在linq中使用tostring()方法
            var app = AppService.GetModel(s => s.Id == id);
            if (app != null) return app.Identity;

            return string.Empty;
        }
    }
}